<template>
	<view>
		<dev-navbar title="健康报告"></dev-navbar>
		<view class="switch-box">
			<view :class="current===0?'activation-bar':'not-active-bar'" @tap="changeType(0)">
				评估报告
			</view>
			<view :class="current===1?'activation-bar':'not-active-bar'" @tap="changeType(1)">
				体检报告
			</view>
		</view>
		<view class="overall-data">
			<!-- 每一份报告 -->
			<view  @longpress="logoTime(item)"  @tap.stop="jumpDetails(item)" class="each-report" v-for="(item,index) in list" :key="index">
				<dev-list-layout leftMove="0" leftFontWight="500" modify-color-right="#029D95" :show-split-lines="false"
					height="42" :leftText="item[keyList[current].name]" leftColor="#029D95" rightTextColor="#029D95"
					:rightText="item[keyList[current].time]" backgroundColor="none" imageHeightRight="30"
					imageWidthRight="30" rightTextWidth="284">
				</dev-list-layout>
				<view class="total-pictures" v-if="current==1 && item.fileType==1">
					共{{item.reportUrl | imageNum}}张图片>
				</view>
			</view>
		</view>
		<nodata v-if="list.length ===0 || !list"></nodata>
	</view>
</template>

<script>
	import devListLayout from '@/pagesMy/components/dev-list-layout/dev-list-layout.vue'
	export default {
		components: {
			devListLayout
		},
		data() {
			return {
				current: 0,
				list: [], //报告数据

				keyList: [{
						name: 'topicName',
						time: 'updateTime',
						api: 'assessmentReport'
					},
					{
						name: 'phyInstitution',
						time: 'createTime',
						api: 'userPhysicalReport'
					}
				],

				pageCurrent: 1,
				pageSize: 10,
				totalPages: '', //总页数
			};
		},
		filters: {
			imageNum: function(value) {
				// console.log(value.split(','))
				return value.split(',').length
			}
		},
		onLoad() {
			if (this.current == 0) {
				this.assessmentReport(); //获取评估报告
			} else {
				this.userPhysicalReport(); //获取体检报告
			}
		},
		onReachBottom() {
			if (this.pageCurrent < this.totalPages) {
				this.pageCurrent++;
				if (this.current === 1) {
					this.userPhysicalReport();
				}
			}
		},
		methods: {
			//监听用户长按
			logoTime(item) {
				console.log(item,'传递过来的参数')
				let _this = this;
				uni.showModal({
					title: '提示',
					content: `您是否要删除《${item[_this.keyList[_this.current].name]}》这份报告吗？`,
					success: function(res) {
						if (res.confirm) {
							//删除报告
							if(_this.current == 0){
								_this.$https.delete(global.apiUrls.reportDeletion,{
									userAnsId:item.userAnsId,
									userId:global.userInfo.userId
								}).then(res => {
									if (res.data.code === 1000) {
										_this.assessmentReport(); //获取评估报告
									}
								})
							}else{
								_this.$https.delete(global.apiUrls.deleteReport(item.userPhyRepId)).then(res => {
									if (res.data.code === 1000) {
										_this.userPhysicalReport(1);
									}
								})
							}	
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			changeType(val) {
				if (this.current !== val) {
					this.current = val;
					if (val == 0) {
						this.assessmentReport();
					} else {
						this.userPhysicalReport();
					}
				}
			},
			//获取体检报告
			assessmentReport() {
				let parameter
				if (this.current == 0) {
					parameter = {
						platform: 1,
						userId: global.userInfo.userId
					}
					this.list = [];
				} else {
					parameter = {
						pageCurrent: this.pageCurrent,
						pageSize: this.pageSize,
						userId: global.userInfo.userId
					}
					if (this.pageCurrent == 1) this.list = [];
				}
				this.$https.get(global.apiUrls[this.keyList[this.current].api], parameter).then(res => {
					if (res.data.code === 1000) {
						// let assessmentList = res.data.result.filter(item=>{
						// 	return item.assessmentStatus==1;
						// })
						this.list = res.data.result
					}
				})
			},

			//获取体检报告
			userPhysicalReport(e) {
				if(e){
					this.list = [];
					this.pageCurrent = 1;
				}
				let parameter
				if (this.current == 0) {
					parameter = {
						platform: 1,
						userId: global.userInfo.userId
					}
					this.list = [];
				} else {
					parameter = {
						pageCurrent: this.pageCurrent,
						pageSize: this.pageSize,
						userId: global.userInfo.userId
					}
					if (this.pageCurrent == 1) this.list = [];
				}

				this.$https.get(global.apiUrls[this.keyList[this.current].api], parameter).then(res => {
					if (res.data.code === 1000) {
						this.list = [...this.list, ...res.data.result.records]
						this.totalPages = res.data.result.pages;
					}
				})
			},
			//跳转到详情界面
			jumpDetails(item) {
                console.info(item,"item---》")
                 console.info(this.current,"this.current》")
				if (this.current == 0) {
					this.$urouter.navigateTo({
						url: item.wechatUrl,
						params: {
							userAnsId: item.userAnsId,
							topicId: item.topicId,
							topicName:item.topicName
						}
					});
				} else {
					if (item.fileType == 1) {
						this.$urouter.navigateTo({
							url: '/pagesMy/health-records/report-details',
							params: {
								url: item.reportUrl
							}
						});
					} else {
						wx.showLoading({
							title: 'pdf预览准备中',
						})
						//这一步是下载PDF
						wx.downloadFile({
							url: item.reportUrl,
							success(res) {
						 	console.log(res)
								const filePath = res.tempFilePath
								setTimeout(function() {
									//这一步是打开PDF
									wx.openDocument({
										fileType: "pdf",
										filePath: filePath,
										showMenu: true,
										success: function(res) {
											console.log(res)
											wx.hideLoading({
												success: function() {
													console.log('关闭');
												}
											})

										}
									})
								}, 300)
							},
							fail: function(e) {
								console.log(e, '网络错误');
							}
						})
					}
				}

			}
		}
	}
</script>

<style lang="scss">
	// 切换栏
	.switch-box {
		display: flex;
		background-color: #fff;

		.activation-bar {
			width: 50%;
			height: 80rpx;
			text-align: center;
			line-height: 80rpx;
			color: #fff;
			font-size: 30rpx;
			font-weight: 400;
			background-color: #2EA7E0;
			border-bottom: 4rpx solid #2EA7E0;
		}

		.not-active-bar {
			width: 50%;
			height: 80rpx;
			text-align: center;
			line-height: 80rpx;
			color: #303030;
			font-size: 30rpx;
			font-weight: 400;
			border-bottom: 4rpx solid #DDDDDD;
		}
	}

	.overall-data {
		padding: 1rpx 24rpx;

		.each-report {
			padding: 22rpx 24rpx;
			margin-top: 30rpx;
			width: 702rpx;
			height: 134rpx;
			background-image: url('http://static.bsyjk.cn/shoppingMall/68F986B4FC4A41E0B9B991BF65A8F47D.png');
			background-size: 100% 100%;

			.total-pictures {
				margin-top: 10rpx;
				font-size: 24rpx;
				font-weight: 400;
				color: #029D95;
			}
		}



	}
</style>
